package com.cmz.common.crypto;

import com.cmz.common.api.crypto.Encryptor;
import com.cmz.common.consts.CommonConsts;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Base64;

/**
 * @author cmz
 */
@Slf4j
public class Base64Encryptor implements Encryptor {

    @Override
    public byte[] encrypt(byte[] bytes) {
        return Base64.getEncoder().encode(bytes);
    }

    @Override
    public byte[] decrypt(byte[] bytes) {
        String decode;
        String msg = null;
        try {
            msg = new String(bytes, CommonConsts.ENCODING_UTF8);
            decode = new String(Base64.getDecoder().decode(msg));
        } catch (IOException e) {
            log.warn("Error occurred while decoding base64 message!");
            return msg.getBytes();
        }
        return decode.getBytes();
    }

}
